const JwtUtil = require('../utils/TokenJwt')
const {enu,Ajax} = require("../utils/Ajax");
const logging = require('../utils/log');

const intercept = (req, res, next) => {
   logging('info',`router into url:${req.url}`)
    const unauthenticatedPaths = ['/login', '/register','/getCode','/'];
    if (unauthenticatedPaths.includes(req.path)) {
        next();
    } else {
        const token = req.headers.authorization;
        if (token) {
            if (JwtUtil.JwtDecoded(token)===false) {
                logging('debug',`token:${req.url}`)
                res.send(Ajax(enu().code.service.fail,enu().msg.service.JwtMsg[1],null))
            } else {
                next()
            }
        }else {
            res.send(Ajax(enu().code.service.fail,enu().msg.service.JwtMsg[0],null))
        }
    }
}

module.exports = intercept
